

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/favicon.jpg">
  <link rel="icon" href="/blog/img/favicon.jpg">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="_SayUw">
  <meta name="keywords" content="html css js">
  
    <meta name="description" content="This 指向  先上总结：this 总是（非严格模式下）指向一个对象，而具体指向哪个对象是在运行时基于函数的执行环境动态绑定的，而非函数被声明时的环境。简单点理解就是谁调用函数就指向谁。  PS：严格模式下（”use strict”），函数的 this 指向的是 undefined。   一、全局调用函数  在全局中调用函数，此时 this 指向 window。 1234567function">
<meta property="og:type" content="article">
<meta property="og:title" content="【知识点 | JS】This 指向、箭头函数">
<meta property="og:url" content="https://sayuw.gitee.io/blog/2022/09/05/jsThis/index.html">
<meta property="og:site_name" content="SayUw&#39;s Blog">
<meta property="og:description" content="This 指向  先上总结：this 总是（非严格模式下）指向一个对象，而具体指向哪个对象是在运行时基于函数的执行环境动态绑定的，而非函数被声明时的环境。简单点理解就是谁调用函数就指向谁。  PS：严格模式下（”use strict”），函数的 this 指向的是 undefined。   一、全局调用函数  在全局中调用函数，此时 this 指向 window。 1234567function">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-09-05T06:00:06.000Z">
<meta property="article:modified_time" content="2022-09-20T06:39:52.126Z">
<meta property="article:author" content="_SayUw">
<meta property="article:tag" content="js">
<meta property="article:tag" content="es6">
<meta name="twitter:card" content="summary_large_image">
  
  
  
  <title>【知识点 | JS】This 指向、箭头函数 - SayUw&#39;s Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"sayuw.gitee.io","root":"/blog/","version":"1.9.2","typing":{"enable":true,"typeSpeed":60,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":3},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/blog/js/utils.js" ></script>
  <script  src="/blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/blog/">
      <strong>_SayUw</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/blog/img/post.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="【知识点 | JS】This 指向、箭头函数"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-09-05 14:00" pubdate>
          2022年9月5日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          4.2k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          36 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">【知识点 | JS】This 指向、箭头函数</h1>
            
            
              <div class="markdown-body">
                
                <h2 id="This 指向"><a href="#This 指向" class="headerlink" title="This 指向"></a>This 指向 </h2><p> 先上总结：this 总是（非严格模式下）指向一个对象，而具体指向哪个对象是在运行时基于函数的执行环境动态绑定的，而非函数被声明时的环境。简单点理解就是谁调用函数就指向谁。</p>
<blockquote>
<p>PS：严格模式下（”use strict”），函数的 this 指向的是 undefined。</p>
</blockquote>
<h3 id="一、全局调用函数"><a href="# 一、全局调用函数" class="headerlink" title="一、全局调用函数"></a> 一、全局调用函数 </h3><p> 在全局中调用函数，此时 this 指向 window。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">fun</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">this</span>);<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">this</span> === <span class="hljs-variable language_">window</span>);<br>&#125;<br><span class="hljs-title function_">fun</span>();<br><span class="hljs-comment">//window 对象 </span><br><span class="hljs-comment">//true</span><br></code></pre></td></tr></table></figure>

<p> 这里在全局作用域中调用的函数，本质上就是 window.fun ()，所以指向的是 window 对象。</p>
<h3 id="二、对象调用函数"><a href="# 二、对象调用函数" class="headerlink" title="二、对象调用函数"></a> 二、对象调用函数 </h3><p> 当函数为对象的一个属性，此时函数内部的 this 指向函数本身。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> <span class="hljs-title class_">Person</span> = &#123;<br>  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;SayUw&#x27;</span>,<br>  <span class="hljs-title function_">say</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;My Name is &#x27;</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>);<br>  &#125;<br>&#125;<br><span class="hljs-title class_">Person</span>.<span class="hljs-title function_">say</span>(); <span class="hljs-comment">// My Name is SayUw</span><br></code></pre></td></tr></table></figure>

<p> 结合一和二的情况，我们改动一下二中的代码：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> name = <span class="hljs-string">&#x27;Window&#x27;</span>;<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">say</span>(<span class="hljs-params"></span>) &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;My Name is &#x27;</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>);<br>&#125;<br><br><span class="hljs-keyword">var</span> <span class="hljs-title class_">Person</span> = &#123;<br>  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;SayUw&#x27;</span>,<br>  say<br>&#125;<br><br><span class="hljs-variable language_">window</span>.<span class="hljs-title function_">say</span>(); <span class="hljs-comment">// My Name is Window</span><br><span class="hljs-variable language_">window</span>.<span class="hljs-property">Person</span>.<span class="hljs-title function_">say</span>(); <span class="hljs-comment">// My Name is SayUw</span><br></code></pre></td></tr></table></figure>

<p> 此时函数是在全局作用域中定义，所以 window.say () 输出的是 window.name，而 window.Person.say () 输出的是 Person.name。其实总结起来还是那句话，谁调用就指向谁。</p>
<h3 id="三、构造函数"><a href="# 三、构造函数" class="headerlink" title="三、构造函数"></a> 三、构造函数 </h3><p> 当 this 出现在构造函数中，this 指向的是新创建的对象。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params">name</span>) &#123;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br>&#125;<br><br><span class="hljs-keyword">var</span> person1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>(<span class="hljs-string">&#x27;SayUw&#x27;</span>);<br><span class="hljs-keyword">var</span> person2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>(<span class="hljs-string">&#x27;Tom&#x27;</span>);<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person1.<span class="hljs-property">name</span>); <span class="hljs-comment">// SayUw</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person2.<span class="hljs-property">name</span>); <span class="hljs-comment">// Tom</span><br></code></pre></td></tr></table></figure>

<h2 id="改变 This 指向"><a href="# 改变 This 指向" class="headerlink" title="改变 This 指向"></a> 改变 This 指向 </h2><p> 想要改变 This 指向，可以使用 apply、call、bind 方法。</p>
<h3 id="一、apply-x2F-call"><a href="# 一、apply-x2F-call" class="headerlink" title="一、apply () &#x2F; call ()"></a> 一、apply () &#x2F; call ()</h3><p>apply 方法传入两个参数，一个是作为函数上下文的对象（也就是 this 要指向的对象），另外一个是作为函数参数所组成的数组。</p>
<p>call 方法第一个参数也是作为函数上下文的对象，但是后面传入的是多个参数，而不是单个数组。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> name = <span class="hljs-string">&#x27;Window&#x27;</span>;<br><span class="hljs-keyword">var</span> <span class="hljs-title class_">Person</span> = &#123;<br>  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;SayUw&#x27;</span>,<br>&#125;<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">say</span>(<span class="hljs-params">age, sex</span>) &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27; 年龄: &#x27;</span> + age);<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27; 性别: &#x27;</span> + sex);<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27; 姓名: &#x27;</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>);<br>&#125;<br><br><span class="hljs-title function_">say</span>(<span class="hljs-string">&#x27;18&#x27;</span>, <span class="hljs-string">&#x27; 男 &#x27;</span>);<br><span class="hljs-comment">// 年龄: 18</span><br><span class="hljs-comment">// 性别：男 </span><br><span class="hljs-comment">// 姓名: Window</span><br>say.<span class="hljs-title function_">apply</span>(<span class="hljs-title class_">Person</span>, [<span class="hljs-string">&#x27;23&#x27;</span>, <span class="hljs-string">&#x27; 男 &#x27;</span>]);<br><span class="hljs-comment">// 年龄: 23</span><br><span class="hljs-comment">// 性别：男 </span><br><span class="hljs-comment">// 姓名: SayUw</span><br>say.<span class="hljs-title function_">call</span>(<span class="hljs-title class_">Person</span>, <span class="hljs-string">&#x27;24&#x27;</span>, <span class="hljs-string">&#x27; 男 &#x27;</span>);<br><span class="hljs-comment">// 年龄: 24</span><br><span class="hljs-comment">// 性别：男 </span><br><span class="hljs-comment">// 姓名: SayUw</span><br></code></pre></td></tr></table></figure>

<p> 第一个方法 this 指向的是 window，所以姓名输出了 Window。</p>
<p>apply 和 call 传入了 Person，所以 this 指向了 Person，所以姓名输出了 SayUw。</p>
<h3 id="二、bind"><a href="# 二、bind" class="headerlink" title="二、bind ()"></a> 二、bind ()</h3><p>bind () 接收的参数和 call () 类似，区别在于 call 是直接执行，而 bind 不执行，而是返回一个指向指定的对象的新的函数。即改变 this 指向后的新函数，旧的函数不变。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> name = <span class="hljs-string">&#x27;Window&#x27;</span>;<br><span class="hljs-keyword">var</span> <span class="hljs-title class_">Person</span> = &#123;<br>  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;SayUw&#x27;</span>,<br>&#125;<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">say</span>(<span class="hljs-params">age, sex</span>) &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27; 年龄: &#x27;</span> + age);<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27; 性别: &#x27;</span> + sex);<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27; 姓名: &#x27;</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>);<br>&#125;<br><br><span class="hljs-keyword">var</span> newSay = say.<span class="hljs-title function_">bind</span>(<span class="hljs-title class_">Person</span>, <span class="hljs-string">&#x27;23&#x27;</span>, <span class="hljs-string">&#x27; 男 &#x27;</span>);<br><br><span class="hljs-title function_">newSay</span>();<br><span class="hljs-comment">// 年龄: 23</span><br><span class="hljs-comment">// 性别：男 </span><br><span class="hljs-comment">// 姓名: SayUw</span><br><span class="hljs-title function_">say</span>(<span class="hljs-string">&#x27;18&#x27;</span>, <span class="hljs-string">&#x27; 男 &#x27;</span>);<br><span class="hljs-comment">// 年龄: 18</span><br><span class="hljs-comment">// 性别：男 </span><br><span class="hljs-comment">// 姓名: Window</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(say === newSay); <span class="hljs-comment">//false</span><br></code></pre></td></tr></table></figure>

<p> 我们可以看到此时 this 的指向已经变成了 Person，say 和 newSay 已经不是两个相同的函数了。</p>
<h2 id="箭头函数"><a href="# 箭头函数" class="headerlink" title="箭头函数"></a> 箭头函数 </h2><p> 在 ES6 中引入了箭头函数的概念，可以用更短的语法去定义函数。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 一个参数，一条返回语句的写法 </span><br><span class="hljs-keyword">var</span> <span class="hljs-title function_">fun</span> = a =&gt; a + <span class="hljs-number">1</span>;<br><span class="hljs-title function_">fun</span>(<span class="hljs-number">1</span>); <span class="hljs-comment">// 2</span><br><br><span class="hljs-comment">// 多个参数，一条返回语句的写法 </span><br><span class="hljs-keyword">var</span> <span class="hljs-title function_">fun</span> = (<span class="hljs-params">a, b</span>) =&gt; a + b;<br><span class="hljs-title function_">fun</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); <span class="hljs-comment">// 3</span><br><br><span class="hljs-comment">// 多个参数，多条返回语句的写法 </span><br><span class="hljs-keyword">var</span> <span class="hljs-title function_">fun</span> = (<span class="hljs-params">a, b</span>) =&gt; &#123;<br>    <span class="hljs-keyword">let</span> res = a + b;<br>    <span class="hljs-keyword">return</span> res;<br>&#125;<br><span class="hljs-title function_">fun</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); <span class="hljs-comment">// 3</span><br></code></pre></td></tr></table></figure>

<p> 使用箭头函数时要注意以下几点：</p>
<ul>
<li><p> 没有自己的 this，它默认指向上一层作用域的 this</p>
</li>
<li><p> 不能使用 apply、call、bind 改变 this 指向 </p>
</li>
<li><p> 不可以作为构造函数，对其使用 new 会报错 </p>
</li>
<li><p> 不可以使用 arguments 对象，可以使用 rest 参数 </p>
</li>
<li><p> 不可以使用 yield 命令，所以不能作为 Generator 函数 </p>
</li>
</ul>
<h3 id="一、没有自己的 this"><a href="# 一、没有自己的 this" class="headerlink" title="一、没有自己的 this"></a> 一、没有自己的 this</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> name = <span class="hljs-string">&#x27;Window&#x27;</span>;<br><span class="hljs-keyword">var</span> <span class="hljs-title class_">Person</span> = &#123;<br>  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;SayUw&#x27;</span>,<br>  <span class="hljs-attr">say</span>: <span class="hljs-function">() =&gt;</span> &#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>);<br>  &#125;<br>&#125;<br><span class="hljs-title class_">Person</span>.<span class="hljs-title function_">say</span>(); <span class="hljs-comment">// Window</span><br></code></pre></td></tr></table></figure>

<p> 因为没有自己的 this，所以指向的是上一层作用域的 this，即 window 对象，所以输出的是 Window 而不是 SayUw。</p>
<h3 id="二、不能使用 apply 等改变 this 指向"><a href="# 二、不能使用 apply 等改变 this 指向" class="headerlink" title="二、不能使用 apply 等改变 this 指向"></a> 二、不能使用 apply 等改变 this 指向 </h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> name = <span class="hljs-string">&#x27;Window&#x27;</span>;<br><span class="hljs-keyword">var</span> <span class="hljs-title class_">Person</span> = &#123;<br>  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;SayUw&#x27;</span>,<br>&#125;<br><br><span class="hljs-keyword">var</span> <span class="hljs-title function_">say</span> = (<span class="hljs-params">age, sex</span>) =&gt; &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27; 年龄: &#x27;</span> + age);<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27; 性别: &#x27;</span> + sex);<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27; 姓名: &#x27;</span> + <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span>);<br>&#125;<br><br>say.<span class="hljs-title function_">apply</span>(<span class="hljs-title class_">Person</span>, [<span class="hljs-string">&#x27;23&#x27;</span>, <span class="hljs-string">&#x27; 男 &#x27;</span>]);<br><span class="hljs-comment">// 年龄: 23</span><br><span class="hljs-comment">// 性别：男 </span><br><span class="hljs-comment">// 姓名: Window</span><br></code></pre></td></tr></table></figure>

<p> 因为不能改变 this 指向，所以此时 this 还是 window，输出 Window。</p>
<h3 id="三、不可以作为构造函数"><a href="# 三、不可以作为构造函数" class="headerlink" title="三、不可以作为构造函数"></a> 三、不可以作为构造函数 </h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> <span class="hljs-title function_">Person</span> = (<span class="hljs-params">name</span>) =&gt; &#123;<br>  <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br>&#125;<br><br><span class="hljs-keyword">var</span> person1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>(<span class="hljs-string">&#x27;SayUw&#x27;</span>); <br><span class="hljs-comment">// Uncaught TypeError: Person is not a constructor</span><br></code></pre></td></tr></table></figure>

<p> 构造函数是通过 new 关键字来生成对象实例，生成对象实例的过程也是通过构造函数给实例绑定 this 的过程，而箭头函数没有自己的 this。</p>
<p> 创建对象过程，new 首先会创建一个空对象，并将这个空对象的__proto__指向构造函数的 prototype，从而继承原型上的方法，但是箭头函数没有 prototype。因此不能使用箭头作为构造函数，也就不能通过 new 操作符来调用箭头函数。</p>
<h3 id="四、不可以使用 arguments 对象"><a href="# 四、不可以使用 arguments 对象" class="headerlink" title="四、不可以使用 arguments 对象"></a> 四、不可以使用 arguments 对象 </h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> <span class="hljs-title function_">Person</span> = (<span class="hljs-params">name</span>) =&gt; &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">arguments</span>[<span class="hljs-number">0</span>]);<br>&#125;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person2</span>(<span class="hljs-params">name</span>) &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">arguments</span>[<span class="hljs-number">0</span>]);<br>&#125;<br><span class="hljs-keyword">var</span> <span class="hljs-title function_">Person3</span> = (<span class="hljs-params">...argus</span>) =&gt; &#123;<br>  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(argus [<span class="hljs-number">0</span>]);<br>&#125;<br><br><span class="hljs-title class_">Person</span>(<span class="hljs-string">&#x27;SayUw&#x27;</span>); <span class="hljs-comment">// Uncaught ReferenceError: arguments is not defined</span><br><span class="hljs-title class_">Person2</span>(<span class="hljs-string">&#x27;SayUw&#x27;</span>); <span class="hljs-comment">// SayUw</span><br><span class="hljs-title class_">Person3</span>(<span class="hljs-string">&#x27;SayUw&#x27;</span>); <span class="hljs-comment">// SayUw</span><br></code></pre></td></tr></table></figure>

<h2 id="参考链接"><a href="# 参考链接" class="headerlink" title="参考链接"></a> 参考链接 </h2><p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/tsl0324/p/14487189.html?ivk_sa=1024320u"> 深入理解 this 的指向问题（6 种全） - 程序员阿田 - 博客园 </a></p>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/jackson1/p/13763477.html">JS 中 apply 、call 、bind 的详解 - jacksonni - 博客园 </a></p>
<p><a target="_blank" rel="noopener" href="https://es6.ruanyifeng.com/?search=% E7% AE% AD% E5% A4% B4% E5%87% BD% E6%95% B0&x=7&y=8#docs/function#% E7% AE% AD% E5% A4% B4% E5%87% BD% E6%95% B0">ES6 入门教程 </a></p>
<hr>
<p> 写在最后：最近会慢慢把平时遇到的一些知识点整理出来（指东一个西一个的笔记），一点点把琐碎的知识点归纳总结，方便以后查询。本文先整理一下 this 指向问题，修改 this 指向的方法，箭头函数相关内容。毕竟知道自己能调用什么变量很重要的，不然我找个叫 A 的对象，不能所有叫 A 的人都是我对象对吧。诶！既然这样，那下次就整理一下和对象相关的原型和原型链吧。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/blog/categories/%E7%9F%A5%E8%AF%86%E7%82%B9/" class="category-chain-item">知识点</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/blog/tags/js/">#js</a>
      
        <a href="/blog/tags/es6/">#es6</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>【知识点 | JS】This 指向、箭头函数</div>
      <div>https://sayuw.gitee.io/blog/2022/09/05/jsThis/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>_SayUw</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年9月5日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/blog/2022/09/20/jsPrototype/" title="【知识点 | JS】原型和原型链">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">【知识点 | JS】原型和原型链</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/blog/2022/08/24/helloHexo/" title="【应用 | Hexo】Gitee Page + Hexo 部署个人博客">
                        <span class="hidden-mobile">【应用 | Hexo】Gitee Page + Hexo 部署个人博客</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
      world.execute(me);
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/events.js" ></script>
<script  src="/blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/blog/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
